package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.LocalDateTimeUtil;
import com.sudo.dao.admin.entity.SysMyStsMonth;
import com.sudo.dao.admin.entity.SysUserBehavior;
import com.sudo.dao.admin.entity.SysUserLoginRecord;
import com.sudo.dao.admin.mapper.SysMyStsMonthMapper;
import com.sudo.dao.admin.mapper.SysUserBehaviorMapper;
import com.sudo.dao.admin.mapper.SysUserLoginRecordMapper;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysMyStsMonthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class SysMyStsMonthServiceImpl extends AdminBaseService implements SysMyStsMonthService {

    @Resource
    private SysUserBehaviorMapper sysUserBehaviorMapper;

    @Resource
    private SysUserLoginRecordMapper sysUserLoginRecordMapper;

    @Resource
    private SysMyStsMonthMapper sysMyStsMonthMapper;


    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void stsMyBehaviorByMonth(int year, int month,String username) {
        Example deleteExample = new Example(SysMyStsMonth.class);
        Example.Criteria criteria = deleteExample.createCriteria();
        criteria.andEqualTo("year", year);
        criteria.andEqualTo("month", month);
        criteria.andEqualTo("username", username);
        sysMyStsMonthMapper.deleteByExample(deleteExample);

        LocalDate monthBegin = LocalDateTimeUtil.parseMonthBegin(year, month);
        LocalDate monthEnd = LocalDateTimeUtil.parseMonthEnd(monthBegin);

        // 某一月的活跃度（操作数）
        SysMyStsMonth sysMyStsMonth = new SysMyStsMonth();
        Example myBehaviorExample = new Example(SysUserBehavior.class);
        Example.Criteria myBehaviorCriteria = myBehaviorExample.createCriteria();
        myBehaviorCriteria.andGreaterThanOrEqualTo("operTime", monthBegin);
        myBehaviorCriteria.andLessThanOrEqualTo("operTime", monthEnd);
        myBehaviorCriteria.andEqualTo("username", username);
        int numBehavior = sysUserBehaviorMapper.selectCountByExample(myBehaviorExample);
        sysMyStsMonth.setNumBehavior(numBehavior);

        // 某一月的登录次数
        Example myLoginNotExample = new Example(SysUserLoginRecord.class);
        Example.Criteria myLoginNotCriteria = myLoginNotExample.createCriteria();
        myLoginNotCriteria.andGreaterThanOrEqualTo("loginTime",monthBegin);
        myLoginNotCriteria.andLessThanOrEqualTo("loginTime", monthEnd);
        myLoginNotCriteria.andEqualTo("username",username);
        int loginNot = sysUserLoginRecordMapper.selectCountByExample(myLoginNotExample);
        sysMyStsMonth.setLoginNot(loginNot);

        LocalDateTime createTime = LocalDateTime.now();
        sysMyStsMonth.setCreateTime(createTime);
        sysMyStsMonth.setYear(year);
        sysMyStsMonth.setMonth(month);
        sysMyStsMonth.setStartDate(monthBegin);
        sysMyStsMonth.setEndDate(monthEnd);
        sysMyStsMonth.setUsername(username);

        sysMyStsMonthMapper.insertSelective(sysMyStsMonth);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public SysMyStsMonth getMyStsMonth(int year, int month,String username) {
        Example example = new Example(SysMyStsMonth.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("year", year);
        criteria.andEqualTo("month", month);
        criteria.andEqualTo("username", username);
        SysMyStsMonth sysMyStsMonth = sysMyStsMonthMapper.selectOneByExample(example);
        if (null == sysMyStsMonth) {
            sysMyStsMonth = new SysMyStsMonth(year, month);
        }
        return sysMyStsMonth;
    }


    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public List<SysMyStsMonth> getSysMyStsMonthList(LocalDate localDate, String username){
        LocalDate localDate1 = localDate.minusMonths(12);
        int startYear = localDate1.getYear();
        int endYear = localDate.getYear();

        Example example = new Example(SysMyStsMonth.class);
        example.orderBy("year").desc();
        example.orderBy("month").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andGreaterThanOrEqualTo("year",startYear);
        criteria.andLessThanOrEqualTo("year",endYear);
        PageHelper.startPage(1, 12);
        List<SysMyStsMonth> sysMyStsMonthList = sysMyStsMonthMapper.selectByExample(example);
        Collections.reverse(sysMyStsMonthList);
        return sysMyStsMonthList;
    }
}
